printHelp(){
	echo "USAGE:" 
	echo " -g get current interface state (wlan or lan)"
	echo " -a To determine whether the need to update the 4G state"
    echo " -c Close 4g network "
    echo " -o Open 4g newtwork"
    echo " -r show current configure info"
    echo " -s get current 4g network run status"

}
action_InsertLog(){
    local fileName
    fileName=$( echo|date '+%Y-%m-%d'Log.txt)
    if [ ! -f $fileName ]
    then
        touch "$fileName"
    fi
   echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]:$?">> $fileName
}
action_isInterface(){
	local  tmp=$( ubus call luci2.network switch_status '{"switch":"mt7620"}' | jq ".ports[$1].link")
}

action_getNetworkCount(){
    local result=$(cat lan4GAutoConvert.json|jq '.networkCount')
    echo $result
}

action_TraversalNetwork(){
    local result=$(action_getNetworkCount)
    local NetworkName
    local state
    local lockFlag="False"
    let result--
    for i in $(seq 0 $result); 
    do
    	NetworkName=$(cat lan4GAutoConvert.json | jq -r ".network[$i].netNumber")
		state=$(action_isInterface $NetworkName)
	if [ "$state" == "true" ]; then
		lockFlag="true"
		break
	fi
    done
    echo $lockFlag
}

#获取４G的状态
action_get4GState(){
    local ret
    ret=$(ubus call network.interface.4G status | jq '.up')
    echo $ret
}
#对4G进行播号
action_Set4GOn(){
    echo "set current 4G On"
    $(ubus call network.interface.4G up)
}
#关闭4G
action_Set4GOff(){
    $(ubus call network.interface.4G down)
}
#echo "fff$flg"


#遍历列表
action_Update4Gstatus(){
	
#	echo "To determine whether the need to update the 4G state"
#	local lanResult=$(action_isInterface lan)
#	local wanResult=$(action_isInterface wan6)
#	echo $wanResult &wanResult
#
#	if [ "$wanResult" == "true" ] || [ "$lanResult" == "true" ];then
#		echo 'hello'
#	else
#		echo 'world'
#	fi

	local tmp
	local lanState
	lanState=$(action_TraversalNetwork)
	echo "lanState $lanState"
	if [ "$lanState" == "true" ]; then
		echo "lan on"
		tmp=$(action_get4GState)
		echo $tmp
		if [ "$tmp" == "true" ]; then
			$(action_Set4GOff)	
		fi
	else
		tmp=$(action_get4GState)
		echo "lan off 4g $tmp"
		if [ "$tmp" == "false" ]; then
			echo "4g flase"
			$(action_Set4GOn)
		fi

	fi
}

#	lanState=$(action_TraversalNetwork)
#	echo "lanState $lanState"
action_getConfigureInfo(){
    echo "Here is the content and current json profile"
    cat lan4GAutoConvert.json | jq 
}
while getopts g:ascor opt
do
	case "$opt" in
	  g)
          action_isInterface $OPTARG 
          ;;

	  a)
		  action_Update4Gstatus ;; 
      c)  
		  action_Set4GOff ;;
      o) 
          action_Set4GOn ;;
      s)  
		  action_getCurrentStatus ;;
      r)
          action_getConfigureInfo ;;
	  ?) 
          clear
          printHelp ;;
	esac
done
